
package br.com.site.model.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import br.com.site.model.dao.DAOException;
import br.com.site.model.dao.DAOFactory;
import br.com.site.model.dao.EstandeDAO;
import br.com.site.validation.Annotation.Validar;


@Entity
public class Estande implements CrudBaseEntity<Estande, Long>, Auditoria {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.MERGE }, targetEntity = br.com.site.model.entity.Centro.class)
	@JoinColumn(name = "centro")
	private Centro centro;

	@Validar
	private String nome;
	
	@Column(columnDefinition="text")
	private String descricao;

	@ManyToOne(fetch = FetchType.LAZY, targetEntity = br.com.site.model.entity.Usuario.class)
	private Usuario coordenador;

	@ManyToMany(fetch = FetchType.LAZY, targetEntity = br.com.site.model.entity.Usuario.class, cascade = { CascadeType.REFRESH })
	@JoinTable(name = "colaborador_estande", joinColumns = { @JoinColumn(name = "estande") }, inverseJoinColumns = { @JoinColumn(name = "colaborador") })
	@NotFound(action = NotFoundAction.IGNORE)
	private List<Usuario> colaboradores = new ArrayList<Usuario>();

	@OneToMany(mappedBy = "estandeLink", fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE })
	private List<Link> links = new ArrayList<Link>();

	@OneToMany(mappedBy = "estandeMural", fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE })
	private List<AvisoMural> mural = new ArrayList<AvisoMural>();

	@OneToMany(mappedBy = "estandeAnexo", fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE })
	private List<Anexo> anexos = new ArrayList<Anexo>();
	
	@OneToMany(mappedBy = "estandePergunta", fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE })
	private List<Pergunta> perguntas = new ArrayList<Pergunta>();

	@OneToMany(mappedBy = "estandeDiscussao", fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE })
	private List<Discurssao> discurssoes;

	private boolean exibir;

	/* Auditoria */
	@Column(name = "loginUsuario", nullable = false)
	private String loginUsuario;

	@Column(name = "dtCriacao", nullable = false)
	private Date dtCriacao;

	@Column(name = "dtModificacao", nullable = false)
	private Date dtModificacao;

	@Column(name = "ativo", nullable = false)
	private boolean ativo;
	
	
	public String getDescricao() {
		return descricao;
	}

	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}

	public List<Usuario> getColaboradores() {
		return colaboradores;
	}

	public void setColaboradores(List<Usuario> colaboradores) {
		this.colaboradores = colaboradores;
	}

	public String getLoginUsuario() {
		return loginUsuario;
	}

	public void setLoginUsuario(String loginUsuario) {
		this.loginUsuario = loginUsuario;
	}

	public Date getDtCriacao() {
		return dtCriacao;
	}

	public void setDtCriacao(Date dtCriacao) {
		this.dtCriacao = dtCriacao;
	}

	public Date getDtModificacao() {
		return dtModificacao;
	}

	public void setDtModificacao(Date dtModificacao) {
		this.dtModificacao = dtModificacao;
	}

	public boolean isAtivo() {
		return ativo;
	}

	public void setAtivo(boolean ativo) {
		this.ativo = ativo;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Centro getCentro() {
		return centro;
	}

	public void setCentro(Centro centro) {
		this.centro = centro;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public Usuario getCoordenador() {
		return coordenador;
	}

	public void setCoordenador(Usuario coordenador) {
		this.coordenador = coordenador;
	}

	public List<Link> getLinks() {
		return links;
	}

	public void setLinks(List<Link> links) {
		this.links = links;
	}

	public List<AvisoMural> getMural() {
		return mural;
	}

	public void setMural(List<AvisoMural> mural) {
		this.mural = mural;
	}

	public List<Anexo> getAnexos() {
		return anexos;
	}

	public void setAnexos(List<Anexo> anexos) {
		this.anexos = anexos;
	}
	
	public boolean isExibir() {
		return exibir;
	}

	public void setExibir(boolean exibir) {
		this.exibir = exibir;
	}

	public List<Pergunta> getPerguntas() {
		return perguntas;
	}

	public void setPerguntas(List<Pergunta> perguntas) {
		this.perguntas = perguntas;
	}

	public List<Discurssao> getDiscurssoes() {
		return discurssoes;
	}

	public void setDiscurssoes(List<Discurssao> discurssoes) {
		this.discurssoes = discurssoes;
	}

	@Override
	public void remove() throws BusinessException {
		DAOFactory factory = DAOFactory.getInstance();
		try {
			factory.txBegin();
			EstandeDAO dao = factory.getDao(EstandeDAO.class);
			dao.remove(this.id);

			factory.txCommit();
		} catch (DAOException e) {
			factory.txRollback();
			throw new BusinessException(e);
		} finally {
			factory.shutdown();
		}

	}

	@Override
	public void save() throws BusinessException {

		DAOFactory factory = DAOFactory.getInstance();
		try {
			factory.txBegin();
			EstandeDAO dao = factory.getDao(EstandeDAO.class);
			dao.save(this);

			factory.txCommit();
		} catch (DAOException e) {
			factory.txRollback();
			throw new BusinessException(e);
		} finally {
			factory.shutdown();
		}
	}

	public static Estande find(Long oid) {
		DAOFactory factory = DAOFactory.getInstance();
		EstandeDAO dao = factory.getDao(EstandeDAO.class);

		return dao.find(oid);
	}

	public static List<Estande> find() {
		DAOFactory factory = DAOFactory.getInstance();
		EstandeDAO dao = factory.getDao(EstandeDAO.class);

		return dao.find();
	}

	public static List<Estande> findByCriteria(Estande estande) {
		DAOFactory factory = DAOFactory.getInstance();
		EstandeDAO dao = factory.getDao(EstandeDAO.class);

		return dao.findByCriteria(estande);
	}

	public static List<Estande> find(int first, int max) {
		DAOFactory factory = DAOFactory.getInstance();
		EstandeDAO dao = factory.getDao(EstandeDAO.class);

		return dao.find(first, max);
	}

	public static List<Estande> findByCriteria(Estande estande, int first,
			int max) {
		DAOFactory factory = DAOFactory.getInstance();
		EstandeDAO dao = factory.getDao(EstandeDAO.class);

		return dao.findByCriteria(estande, first, max);

	}

}
